package com.shop2cn.iapi.pretrade.testcase;

import java.math.BigDecimal;
import java.util.HashMap;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.pretrade.paremeter.ChargeReq;
import com.shop2cn.iapi.pretrade.service.ChargeCall;
import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.shenqi.DGSQBuildProxyChainCallService;
import com.ymttest.business.service.shenqi.bean.ShenqiUserSet;
import com.ymttest.database.model.AccountEntry;
import com.ymttest.database.sqlwapper.AccountWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.TestDataManager;

/**
 * 自动确认收货提醒通知FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_Charge {

	private static ChargeReq chargeReq;
	private static ChargeCall chargeCall;
	
	private static long virtualCoinGoodsOwnerId = ShenqiUserSet.virtualCoinGoodsOwnerId;
	private static AccountWapper accountWapper = new AccountWapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("代币充值");
	}

	@Before
	public void caseUp() {
		chargeReq = new ChargeReq();
		chargeCall = new ChargeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(chargeCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verify() {

		HashMap<String, Object> accountEntryMap = new HashMap<>();

		accountEntryMap.put("userId", chargeReq.getAgencyId());
		accountEntryMap.put("bizcode", "300052");
		accountEntryMap.put("originalNo", chargeReq.getGoodsOwnerId());
		accountEntryMap.put("accountType", "93");
		accountEntryMap.put("bizno", chargeReq.getUniqueCode());

		AccountEntry accountEntry = accountWapper.selectAccountEntryByMap(
				accountEntryMap).get(0);

		Logger.verifyEquals(chargeReq.getMemo(), accountEntry.getMemo(),
				"验证memo");
		Logger.verifyEquals(chargeReq.getOperator(),
				accountEntry.getOperator(), "验证Operator");
		DataUtil.verifyDecimalEquals(chargeReq.getAmount(),
				accountEntry.getAmount(), "验证Amount");
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Charge_001() {
		Logger.start(true, "货头给代理商充值代币，充值成功");
		try {
			
			AgentProductBean agentProductBean = DGSQBuildProxyChainCallService
					.createIndependenceProxychain(
							(int) virtualCoinGoodsOwnerId, 1, false, false);

			chargeReq.setGoodsOwnerId(virtualCoinGoodsOwnerId);
			chargeReq.setAgencyId(agentProductBean.getSellerId());
			
//			chargeReq.setGoodsOwnerId(500772144L);
//			chargeReq.setAgencyId(500041475L);
			
			chargeReq.setAmount(new BigDecimal("10000.01"));
			chargeReq.setCsId(virtualCoinGoodsOwnerId);
			chargeReq.setMemo("充值");
			chargeReq.setOperator("operato");
			chargeReq.setUniqueCode(TestDataManager.getRandomUUID().toString());

			// 发送请求
			chargeCall.setData(chargeReq);
			chargeCall.callService();

			Logger.verifyEquals(true, chargeCall.getSuccess(), "IsSuccess");
			
			this.verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Charge_002() {
		Logger.start(true, "客服给代理商充值代币，没有权限，充值失败");
		try {
			
			AgentProductBean agentProductBean = DGSQBuildProxyChainCallService
					.createIndependenceProxychain(
							(int) virtualCoinGoodsOwnerId, 1, false, false);

			chargeReq.setGoodsOwnerId(virtualCoinGoodsOwnerId);
			chargeReq.setAgencyId(agentProductBean.getSellerId());
			chargeReq.setAmount(BigDecimal.ONE);
			chargeReq.setCsId(1234L);
			chargeReq.setMemo("充值");
			chargeReq.setOperator("operato");
			chargeReq.setUniqueCode(TestDataManager.getRandomUUID().toString());

			// 发送请求
			chargeCall.setData(chargeReq);
			chargeCall.callService();

			Logger.verifyEquals(false, chargeCall.getSuccess(), "IsSuccess");
			Logger.verifyEquals("无权操作", chargeCall.getMsg(), "Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Charge_003() {
		Logger.start(true, "货头给其它的代理商充值代币，充值失败");
		try {
		
			chargeReq.setGoodsOwnerId(virtualCoinGoodsOwnerId);
			chargeReq.setAgencyId(502101120L);
			chargeReq.setAmount(BigDecimal.ONE);
			chargeReq.setCsId(virtualCoinGoodsOwnerId);
			chargeReq.setMemo("充值");
			chargeReq.setOperator("operato");
			chargeReq.setUniqueCode(TestDataManager.getRandomUUID().toString());

			// 发送请求
			chargeCall.setData(chargeReq);
			chargeCall.callService();

			Logger.verifyEquals(false, chargeCall.getSuccess(), "IsSuccess");
			Logger.verifyEquals("代理商[ 502101120 ]不存在", chargeCall.getMsg(), "Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Charge_004() {
		Logger.start(true, "货头给代理商充值代币，代币金额为负数，充值失败");
		try {
		
			chargeReq.setGoodsOwnerId(virtualCoinGoodsOwnerId);
			chargeReq.setAgencyId(502101120L);
			chargeReq.setAmount(BigDecimal.ONE.negate());
			chargeReq.setCsId(virtualCoinGoodsOwnerId);
			chargeReq.setMemo("充值");
			chargeReq.setOperator("operato");
			chargeReq.setUniqueCode(TestDataManager.getRandomUUID().toString());

			// 发送请求
			chargeCall.setData(chargeReq);
			chargeCall.callService();

			Logger.verifyEquals(false, chargeCall.getSuccess(), "IsSuccess");
			Logger.verifyEquals("充值金额不能小于 0 或等于 0", chargeCall.getMsg(), "Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Charge_005() {
		Logger.start(true, "货头给代理商充值代币，代币金额为0，充值失败");
		try {
		
			chargeReq.setGoodsOwnerId(virtualCoinGoodsOwnerId);
			chargeReq.setAgencyId(502101120L);
			chargeReq.setAmount(BigDecimal.ZERO);
			chargeReq.setCsId(502101120L);
			chargeReq.setMemo("充值");
			chargeReq.setOperator("operato");
			chargeReq.setUniqueCode(TestDataManager.getRandomUUID().toString());

			// 发送请求
			chargeCall.setData(chargeReq);
			chargeCall.callService();

			Logger.verifyEquals(false, chargeCall.getSuccess(), "IsSuccess");
			Logger.verifyEquals("充值金额不能小于 0 或等于 0", chargeCall.getMsg(), "Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Charge_006() {
		Logger.start(true, "非独立云主商户给代理商充值代币，充值失败");
		try {
		
			AgentProductBean agentProductBean = DGSQBuildProxyChainCallService
					.createProxychain((int) 500002490, 1, false);

			chargeReq.setGoodsOwnerId(500002490L);
			chargeReq.setAgencyId(agentProductBean.getSellerId());
			chargeReq.setAmount(new BigDecimal("1.01"));
			chargeReq.setCsId(500002490L);
			chargeReq.setMemo("充值");
			chargeReq.setOperator("operato");
			chargeReq.setUniqueCode(TestDataManager.getRandomUUID().toString());

			// 发送请求
			chargeCall.setData(chargeReq);
			chargeCall.callService();

			Logger.verifyEquals(false, chargeCall.getSuccess(), "IsSuccess");
			Logger.verifyEquals("独立云货头[ 500002490 ]不存在", chargeCall.getMsg(), "Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
